from common.base import *
import re

def guild_sign_in(dm,hwnd):
    number = get_number_from_title(hwnd)
    # 1.底部 + 和 帮派
    # 结果 ＝ 找图点击 (序号, 947, 685, 1022, 757, “首页-底部+.bmp”, 2)
    fale = False
    x,y = dm.Find_pic( 947, 685, 1024, 768,"首页-底部+.bmp","000000",0.8,0)
    if x != -1 and y != -1:
        dm.LeftClick(x,y,10,10)
        for i in range(5):
            sleep(2)
            x, y = dm.Find_pic(463, 600, 1024, 768,"首页-底部-帮派.bmp","000000",0.8,0)
            if x != -1 and y != -1:
                dm.LeftClick(x,y,5,5)
                sleep(2)
                fale = True
                break
            else:
                log.info(f"设备{number}，未找到，首页-底部-帮派.bmp")
    else:
        log.info(f"设备{number}，未找到，首页-底部+.bmp")


    # 2.帮派签到
    if fale:
        sleep(5)
        pic_path = dm.Capture(876,308,1017,560, f"test{number}.bmp")
        retx,rety = OCR_get_text(pic_path,"福|利")
        if retx != -1:
            retx = retx+876
            rety = rety+308
            print([retx, rety])
            dm.LeftClick(retx,rety,10,10)
        else:
            log.info(f"设备{number}，未找到，帮派-福利")
        # 3.签到
        sleep(5)
        pic_path = dm.Capture(733,450,908,542, f"test{number}.bmp")
        retx,rety = OCR_get_text(pic_path,"签|到")
        if retx != -1:
            retx = retx+733
            rety = rety+450
            print([retx,rety])
            dm.LeftClick(retx,rety,10,10)
        else:
            log.info(f"设备{number}，未找到，帮派-签到")
        # 4.领取分红
        fale = False
        sleep(2)
        pic_path = dm.Capture(736,587,915,681, f"test{number}.bmp")
        retx,rety = OCR_get_text(pic_path,"领取|分红")
        if retx != -1:
            retx = retx+736
            rety = rety+587
            print([retx, rety])
            dm.LeftClick(retx,rety,10,10)
            fale =True
        else:
            log.info(f"设备{number}，未找到，帮派-领取分红")
        # 4.领取分红-领取
        if fale:
            for i in range(3):
                sleep(2)
                pic_path = dm.Capture(410,527,637,615, f"test{number}.bmp")
                retx,rety = OCR_get_text(pic_path,"领|取")
                if retx != -1:
                    retx = retx+410
                    rety = rety+527
                    print([retx, rety])
                    dm.LeftClick(retx,rety,10,10)
                    break
                else:
                    log.info(f"设备{number}，未找到，帮派-领取分红-领取")
    # 5.帮派-关闭
    for i in range(5):
        sleep(5)
        x, y = dm.Find_pic(882,53,1003,162, "帮派-关闭X.bmp", "000000", 0.8, 0)
        if x != -1 and y != -1:
            dm.LeftClick(x, y, 5, 5)
            sleep(2)
            break
        else:
            log.info(f"设备{number}，未找到，首页-底部-帮派.bmp")